package com.zzzm.hospital.ui.activity;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.zzzm.hospital.R;
import com.zzzm.hospital.ui.fragment.AllFragment;
import com.zzzm.hospital.ui.fragment.MyselfFragment;
import com.zzzm.hospital.ui.fragment.ServiceFragment;

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

public class MainActivity extends AppCompatActivity {

    private final List<Fragment> homeFragmentList = Arrays.asList(
            new ServiceFragment(), new AllFragment(), new MyselfFragment()
    );

    private BottomNavigationView bnv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

    }

    private void initView() {
        bnv = findViewById(R.id.navi_bottom);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

        //第一次加载页面让第一个Fragment显示
        homeFragmentList.forEach(fragment -> ft.add(R.id.home_fragment_container, fragment).hide(fragment));
        ft.show(homeFragmentList.get(0));
        ft.commit();

        selectNaviBottom();
    }

    private void selectNaviBottom() {
        bnv.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                showSelectedFragment(item.getItemId());
                return true;
            }
        });
    }

    private void showSelectedFragment(int itemId) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

        homeFragmentList.forEach(ft::hide);

        switch (itemId) {
            case R.id.menu_service:
                ft.show(homeFragmentList.get(0));
                break;
            case R.id.menu_all:
                ft.show(homeFragmentList.get(1));
                break;
            case R.id.menu_myself:
                ft.show(homeFragmentList.get(2));
                break;
        }
        ft.commit();
    }

}